package com.ppt.android.common.net.error

import android.net.ParseException
import com.google.gson.JsonParseException
import com.google.gson.stream.MalformedJsonException
import com.ppt.android.base.utils.toast
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException

/**
 * 统一错误处理工具类
 * @author ZouJiaCai
 * @since 2023/6/1 16:37
 */
object ExceptionHandler {
    fun handleException(e: Throwable){
        when(e){
            is HttpException -> {
                when(e.code()){
                    else -> {
                        toast("HttpException code: ${e.code()}")
                    }
                }
            }
            is JsonParseException,is JSONException,is ParseException,is MalformedJsonException ->{
                toast("解析异常")
            }
            is ConnectException ->{
                toast("网络异常，请尝试刷新")
            }
            is javax.net.ssl.SSLException -> {
                toast("证书出错")
            }
            is java.net.UnknownHostException -> {
                toast("网络连接异常~请稍后再试")
            }
            is java.net.SocketException -> {
                toast("连接超时")
            }
            is java.net.SocketTimeoutException -> {
                toast("连接超时")
            }
            else -> {
                toast("异常: ${e.localizedMessage}")
            }
        }
    }
}